package netctoss.demo.controller;

import netctoss.demo.entrty.Account;
import netctoss.demo.entrty.Result;
import netctoss.demo.entrty.Service;
import netctoss.demo.mapper.ServiceMapper;
import netctoss.demo.service.AccountService;
import netctoss.demo.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@RestController
@RequestMapping("service")
public class ServiceController {
    @Autowired
    ServiceService serviceService;

    @Autowired
    AccountService accountService;

    @RequestMapping("")
    public ModelAndView serviceList(){
        return new ModelAndView("service/service_list.html");
    }

    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("/service/service_modi.html");
    }

    @RequestMapping("doUpdate")
    public Result doUpdate(@RequestBody Service service){
        return new Result(true,"更新成功",serviceService.updateByPrimaryKey(service));
    }

    @RequestMapping("/detail")
    public ModelAndView showDetail(){
        return new ModelAndView("/service/service_detail.html");
    }

    @RequestMapping("/insert")
    public ModelAndView insertService(){
        return new ModelAndView("/service/service_add.html");
    }

    @RequestMapping("deleteOne")
    public Result deletOne(@RequestBody Service service){
        service.setServiceStatus("2");
        return new Result(true,"删除成功",serviceService.updateByPrimaryKey(service));
    }

    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable("pageNum")int pageNum,@PathVariable("pageSize")int pageSize, @RequestBody Service condition){
        if (condition.getUnixHost()!=null){
        if (condition.getUnixHost().equals("")){
            condition.setUnixHost(null);
        }}
        if (condition.getOsUsername()!=null){
        if (condition.getOsUsername().equals("")){
            condition.setOsUsername(null);
        }}
        if (condition.getAccountIdcardNo()!=null){
        if (condition.getAccountIdcardNo().equals("")){
            condition.setAccountIdcardNo(null);
        }}
        if (condition.getServiceStatus()!=null){
        if (condition.getServiceStatus().equals("")){
            condition.setServiceStatus(null);
        }}
        return new Result(true,"查询成功",serviceService.selectPage(pageNum,pageSize,condition));

    }

    @RequestMapping("checkAccountName")
    public int checkAccountName(String aname,String id){
        Account account = new Account();
        account.setAccountRealName(aname);
        account.setAccountIdcardNo(id);
        if (accountService.selectOne(account)!=null){
            return 1;
        }
        return 0;
    }

    @RequestMapping("checkAccountIdcardNo")
    public int checkAccountIdcardNo(String id){
        Account account = new Account();
        account.setAccountIdcardNo(id);
        if (accountService.selectOne(account)!=null){
            return 1;
        }
        return 0;
    }

    @RequestMapping("checkUnixHost")
    public int checkUnixHost(String unixHost){
        if(unixHost.startsWith("192.168.0.")) {
            String str = unixHost.substring(10);
            if (str.length()!=0&&str.length()<=3) {
                if (str.equals("20")||str.equals("200")||str.equals("23")||str.equals("26")){
                    return 1;
                }
            }
        }
        return 0;
    }

    @RequestMapping("checkOsUsername")
    public int checkOsUsername(String osUsername){
        if ((!osUsername.equals(""))&&osUsername.length()!=0&&osUsername.length()<=8){
            byte[] bytes = osUsername.getBytes();
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                if ((b>=65&&b<=90)||(b>=97&&b<=122)||b==95||(b>=48&&b<=57)){

                }else {
                    return 0;
                }
            }
            return 1;
        }
        return 0;
    }

    @RequestMapping("checkPassword")
    public int checkPassword(String pwd){
        if ((!pwd.equals(""))&&pwd.length()!=0&&pwd.length()<=30){
            byte[] bytes = pwd.getBytes();
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                if ((b>=65&&b<=90)||(b>=97&&b<=122)||b==95||(b>=48&&b<=57)){
                }else {
                    return 0;
                }
            }
            return 1;
        }
        return 0;
    }

    @RequestMapping("checkPasswordMore")
    public int checkPasswordMore(String pwd,String pwd2){
        if (pwd.equals(pwd2)){
        return 1;}
        return 0;
    }

    @RequestMapping("doInsert")
    public int insertService(@RequestBody Service service){
        Account account = new Account();
        account.setAccountIdcardNo(service.getAccountIdcardNo()+"");
         account = accountService.selectOne(account);
         service.setAccountId(Math.toIntExact(account.getAccountId()));
        service.setServiceCreateDate(new Date());
        return serviceService.insertSelective(service);
    }
}
